package com.ywyself.ext.kit;

import java.util.HashMap;

import com.jfinal.core.Controller;
import com.jfinal.json.Json;

/**
 * 数据传递，程序内部，控制层与客户机的数据传输
 * 
 * @author admin
 * @date 2017年10月18日 下午4:58:51
 */
@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
public class YsKit extends HashMap {

    private static final String STATE = "state";
    private static final String STATE_OK = "ok";
    private static final String STATE_FAIL = "fail";

    public YsKit setOk() {
        super.put(STATE, STATE_OK);
        return this;
    }

    public YsKit setFail() {
        super.put(STATE, STATE_FAIL);
        return this;
    }

    public boolean isOk() {
        if (STATE_OK.equals(get(STATE))) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isFail() {
        if (STATE_FAIL.equals(get(STATE))) {
            return true;
        } else {
            return false;
        }
    }

    public static YsKit ok() {
        return new YsKit().setOk();
    }

    public static YsKit fail() {
        return new YsKit().setFail();
    }

    /**
     * code
     * 
     * @param code
     * @return
     */
    public YsKit code(String code) {
        super.put("code", code);
        return this;
    }

    /**
     * 信息
     */
    public YsKit msg(String msg) {
        super.put("msg", msg);
        return this;
    }

    /**
     * 获取数据
     * 
     * @param <T>
     * 
     * @return
     */
    public <T> T getData() {
        return (T) super.get("data");
    }

    /**
     * 获取信息
     * 
     * @param msg
     */
    public String getMsg() {
        Object s = get("msg");
        return s != null ? s.toString() : null;
    }

    /**
     * 数据
     * 
     * @param <T>
     */
    public <T> YsKit data(T data) {
        super.put("data", data);
        return this;
    }

    /**
     * 移除data
     * 
     * @return
     */
    public YsKit removeData() {
        super.remove("data");
        return this;
    }

    public YsKit set(Object key, Object value) {
        super.put(key, value);
        return this;
    }

    public String getStr(Object key) {
        Object s = get(key);
        return s != null ? s.toString() : null;
    }

    public Integer getInt(Object key) {
        Number n = (Number) get(key);
        return n != null ? n.intValue() : null;
    }

    public Long getLong(Object key) {
        Number n = (Number) get(key);
        return n != null ? n.longValue() : null;
    }

    public Number getNumber(Object key) {
        return (Number) get(key);
    }

    public Boolean getBoolean(Object key) {
        return (Boolean) get(key);
    }

    public String toJson() {
        return Json.getJson().toJson(this);
    }

    /**
     * sql语句执行出错
     */
    public static YsKit sqlError() {
        return new YsKit().setFail().msg("sql语句执行出错");
    }

    public void render(Controller controller) {
        controller.renderJson(toJson());
    }
}
